package service

import (
	"fmt"
	"goapp-web-server/app/models"
	"goapp-web-server/app/utils/orm"
)

type CmsCategory struct {
	models.CmsCategory `xorm:"extends"`
	CommentCount       int32 `json:"comment_count" xorm:"comment_count"`
	ZanCount           int32 `json:"zan_count" xorm:"zan_count"`
}
type CmsCategoryService interface {
	GetAll() ([]CmsCategory, error)
	GetByID(id int64) models.CmsCategory
}

func NewCategoryService() CmsCategoryService {
	return new(categoryService)
}

type categoryService struct {
}

// category list
func (s *categoryService) GetAll() ([]CmsCategory, error) {
	engine := orm.Engine
	list := make([]CmsCategory, 0)
	err := engine.Table("cms_category").
		Select(`cms_category.*`).
		Where("cms_category.deleted_at is NULL").
		Limit(10, 0).
		Find(&list)
	if err != nil {
		fmt.Println("error:", err)
		return nil, err
	}
	return list, nil
}

// get category by id
func (s *categoryService) GetByID(id int64) models.CmsCategory {
	category := models.CmsCategory{}
	fmt.Println(category.CategoryName)
	return category
}
